function formatDate(pattern, options) {
    // pattern = "YYYY-MM-DD hh:mm:ss"
    // console.log(pattern, definedDate, isBeautify);

    options = options != undefined ? options : {};


    if (options.definedDate != undefined) { // 有第二参数(自行指定时间)
        var date = new Date(options.definedDate);
    } else {
        var date = new Date();
    }

    // 有第三参数
    options.isBeautify = options.isBeautify != undefined ? options.isBeautify : true;

    var year = date.getFullYear();
    var month = date.getMonth() + 1;
    var day = date.getDate();
    var hour = date.getHours();
    var minute = date.getMinutes();
    var second = date.getSeconds();

    //返回替换后的新字符串,对原字符串没有影响 = > 把替换后的新字符串赋值给原值
    // pattern = pattern.replace("YYYY", year);
    // pattern = pattern.replace("MM", month);
    // pattern = pattern.replace("DD", day);
    // pattern = pattern.replace("hh", hour);
    // pattern = pattern.replace("mm", minute);
    // pattern = pattern.replace("ss", second);
    // 2022-6-DD hh:mm:ss 
    // console.log(pattern);


    // 返回替换后的新字符串 => 继续替换

    if (options.isBeautify) {
        pattern = pattern.replace("YYYY", year).replace("MM", beautify(month)).replace("DD", beautify(day)).replace("hh", beautify(hour)).replace("mm", beautify(minute)).replace("ss", beautify(second));
    } else {
        pattern = pattern.replace("YYYY", year).replace("MM", month).replace("DD", day).replace("hh", hour).replace("mm", minute).replace("ss", second);
    }

    return pattern;


    function beautify(num) {
        if (isNaN(num)) {
            var err = new Error("Please pass in a number");
            throw err;
        }
        return num < 10 ? "0" + num : num;
    }
}